<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class shop_admin extends admin
{

	private $db;
  public $username;
  /**
   * 构造函数
   */
	public function __construct()
	{
		parent::__construct();
		//if (!module_exists(ROUTE_M)) showmessage(L('module_not_exists'));
		$this->username = param::get_cookie('admin_username');
		$this->db = pc_base::load_model('shop_model');
	}

  /**
   * 入口函数，未确认列表页输出视图
   * @return [type] [description]
   */
	public function init()
	{
		$sql = '`siteid`=\''.$this->get_siteid().'\' AND `passed`=0';

		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`sid` DESC', $page, $pagesize = 20);
		$pages = $this->db->pages;
    include $this->admin_tpl('shop_list');
	}

	/**
	 * 已确认收款列表页输出视图
	 * @return [type] [description]
	 */
	public function payment_list()
	{
		$sql = '`siteid`=\''.$this->get_siteid().'\' AND `passed`=1';

		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`sid` DESC', $page, $pagesize = 20);
		$pages = $this->db->pages;
    include $this->admin_tpl('shop_paymentlist');
	}
	/**
	 * 确认收款函数
	 * @return [type] [description]
	 */
	public function apply()
	{
		$_GET['sid'] = intval($_GET['sid']);
		if(!$_GET['sid']) showmessage(L('illegal_operation'));

		//是则写入数据，否则调取数据显示
		if(isset($_POST['dosubmit'])) {
			if ($_POST['tradeno'] != '') {
				if($this->db->update('passed=1', array('sid' => $_GET['sid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'apply');
			}
			else {
				showmessage('缺少单号，无法确认');
			}
			// $_POST['guestbook'] = $this->check($_POST['guestbook'], 'reply');
		}
		else {
			$where = array('sid' => $_GET['sid']);
			$shop_info = $this->db->get_one($where);

			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$template_list = template_list($this->siteid, 0);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}

			$show_header = $show_validator = $show_scroll = 1;
			$_username = param::get_cookie('admin_username');

			include $this->admin_tpl('shop_apply');
		}
	}

	/**
	 * 发货函数
	 * @return [type] [description]
	 */
	public function deliver()
	{
		$_GET['sid'] = intval($_GET['sid']);
		if(!$_GET['sid']) showmessage(L('illegal_operation'));

		//是则写入数据，否则调取数据显示
		if(isset($_POST['dosubmit'])) {
			$tid = $_POST['trackingid'];

			if ($tid != '') {
				if($this->db->update('trackingid='.$tid, array('sid' => $_GET['sid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'deliver');
			}
			else {
				showmessage('缺少运单号，无法确认');
			}
		}
		else {
			$where = array('sid' => $_GET['sid']);
			$shop_info = $this->db->get_one($where);

			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$template_list = template_list($this->siteid, 0);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}

			$show_header = $show_validator = $show_scroll = 1;
			$_username = param::get_cookie('admin_username');

			include $this->admin_tpl('shop_deliver');
		}
	}

	/**
	 * 批量删除订单
	 * @param  integer $sid [description]
	 * @return [type]       [description]
	 */
	public function delete($sid = 0)
	{
		if((!isset($_POST['sid']) || empty($_POST['sid'])) && !$sid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['sid']) && !$sid) {
				array_map(array($this, 'delete'), $_POST['sid']);
				showmessage(L('shop_deleted'), HTTP_REFERER);
			} elseif($sid) {
				$sid = intval($sid);
				$this->db->delete(array('sid' => $sid));
			}
		}
	}

	/**
	 * 取消付款确认
	 * @param  integer $sid [description]
	 * @return [type]       [description]
	 */
	public function cancel($sid = 0)
	{
		if((!isset($_POST['sid']) || empty($_POST['sid'])) && !$sid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['sid']) && !$sid) {
				array_map(array($this, 'cancel'), $_POST['sid']);
				showmessage(L('shop_cancel'), HTTP_REFERER);
			} elseif($sid) {
				$sid = intval($sid);
				$this->db->update(array('passed' => 0), array('sid' => $sid));
				return true;
			}
		}
	}

}
?>
